package com.inspector.app.ui.chat.model;

import android.os.Handler;
import android.os.Looper;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;

import com.example.commen.net.easyHttp.EasyHttpManager;
import com.example.commen.net.easyHttp.model.HttpData;
import com.hjq.http.listener.HttpCallback;

import java.util.ArrayList;
import java.util.List;

public class StateRequestManager implements LifecycleEventObserver {
    private static final List<Observer> observers = new ArrayList<>();
    private final Handler handler = new Handler(Looper.getMainLooper());
    private Runnable requestTask;
    private static final long INTERVAL = 10000; // 10秒
    private LifecycleOwner lifecycleOwner;
    private String taskId;

    // 使用构建器模式
    private StateRequestManager(Builder builder) {
        if (builder != null) {
            this.lifecycleOwner = builder.lifecycleOwner;
            this.taskId = builder.taskId;
            lifecycleOwner.getLifecycle().addObserver(this);
            requestTask = new Runnable() {
                @Override
                public void run() {
                    performRequest();
                    handler.postDelayed(this, INTERVAL);
                }
            };
            handler.post(requestTask);
        }
    }

    public static class Builder {
        private LifecycleOwner lifecycleOwner;
        private String taskId;

        public Builder setLifecycleOwner(LifecycleOwner lifecycleOwner) {
            this.lifecycleOwner = lifecycleOwner;
            return this;
        }

        public Builder setTaskId(String taskId) {
            this.taskId = taskId;
            return this;
        }

        public Builder addObserver(Observer observer) {
            synchronized (observers) {
                if (!observers.contains(observer)) {
                    observers.add(observer);
                }
            }
            return this;
        }

        public StateRequestManager build() {

            return new StateRequestManager(this);
        }
    }


    private void performRequest() {
        EasyHttpManager.get().get(lifecycleOwner, "/api/v2/cooperationTask/status?taskId=" + taskId, new HttpCallback<HttpData<String>>() {
            @Override
            public void onSucceed(HttpData<String> result) {
                if (result.getCode() == 200) {
                    String data = result.getData();
                    notifyObservers(data);
                }
            }

            @Override
            public void onFail(Exception e) {// 处理请求失败
                notifyObservers("-1");

            }
        });
    }


    private void notifyObservers(String data) {
        synchronized (observers) {
            for (Observer observer : observers) {
                observer.onDataReceived(data);
            }
        }
    }

    public interface Observer {
        void onDataReceived(String data);
    }


    @Override
    public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
        if (event == Lifecycle.Event.ON_DESTROY) {
            cancel();
        }
    }

    private void cancel() {
        handler.removeCallbacks(requestTask);
        lifecycleOwner.getLifecycle().removeObserver(this);
        synchronized (observers) {
            observers.clear();
        }
    }
}

